home *** CD-ROM | disk | FTP | other *** search
- /**************************************************************************************
- * scroll subroutine - Adapted from MPW toolkit C examples
- *
- * program description
- *
- * Display a window with help information and add a vertical scroll bar to it
- *
- * argument descriptions
- *
- * argument use
- * -------- ---
- *
- * winres The resource id of the window resource.
- *
- * vscres The resource id of the scroll bar control.
- *
- ***************************************************************************************/
-
- /********************
- * Standard C includes
- *********************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- /***************************
- * Macintosh toolbox includes
- ****************************/
- #include <values.h>
- #include <traps.h>
- #include <Packages.h>
-
- /*****************
- * Global variables
- ******************/
- static TEHandle docTE;
- static ControlHandle docVScroll;
- static ProcPtr docClik;
-
- /*pascal void PascalClikLoop();
- pascal ProcPtr GetOldClikLoop();*/
-
- void scroll (char *hlpfil,char *prognam,long int *winres,long int *vscres)
- {
- WindowPtr window;
- Rect viewRect,destRect;
- char bufr[80],txtfil[8000];
- char *txt;
- char pname[9];
- long int txtlen;
- extern long int resnumq;
- long int hlpnum = 400;
- int nlines,i;
- FILE *hlp;
-
- void errmsg();
- void GetTRect();
- void AdjustVwRect();
- void EvntLoop();
- pascal void AsmClikLoop();
-
- txt = txtfil;
- strcpy(txt,"");
-
- hlp = fopen(hlpfil,"rb");
- if (hlp == NULL) {
- errmsg("Error opening the help text file",&hlpnum);
- return;
- }
-
- fgets(bufr,80,hlp);
- while (!feof(hlp)) {
- strcat(txt,bufr);
- fgets(bufr,80,hlp);
- }
- i = fclose(hlp);
-
- window = GetNewWindow(*winres,NULL,(WindowPtr) -1);
-
- if (window != NULL) {
- SetPort(window);
- TextFont(4);
- TextSize(12);
- strcpy(pname,prognam);
- SetWTitle(window,c2pstr(pname));
- GetTRect(window,&viewRect);
- destRect = viewRect;
- destRect.bottom = destRect.top + 100;
- docTE = TENew(&destRect,&viewRect);
-
- if (docTE != NULL) {
- AdjustVwRect(docTE);
- TEAutoView(true,docTE);
- docClik = (ProcPtr) (*docTE)->clikLoop;
- (*docTE)->clikLoop = (ClikLoopProcPtr) AsmClikLoop;
- docVScroll = GetNewControl(*vscres,window);
-
- if (docVScroll != NULL) {
- txtlen = strlen(txt);
- TESetText(txt,txtlen,docTE);
- nlines = (*docTE)->nLines - 27;
- if (nlines < 0) nlines = 0;
- SetCtlMin(docVScroll,0);
- SetCtlMax(docVScroll,nlines);
- SetCtlValue(docVScroll,0);
- TEUpdate(&viewRect,docTE);
- ShowWindow(window);
- ShowControl(docVScroll);
- DrawControls(window);
- HiliteControl(docVScroll,0);
- }
- else {
- errmsg("Error reading control template from resource file",&resnumq);
- DisposeWindow(window);
- exit(0);
- }
- }
- else {
- errmsg("Error creating an edit record for the window",&resnumq);
- DisposeWindow(window);
- exit(0);
- }
- }
- else {
- errmsg("Error reading window template from the resource file",&resnumq);
- DisposeWindow(window);
- exit(0);
- }
-
- EvntLoop();
- }
-
- /******************************
- * GetTRect subroutine
- *
- * subroutine description
- *
- * argument descriptions
- *
- *******************************/
-
- void GetTRect (WindowPtr window,Rect *teRect)
- {
- *teRect = window->portRect;
- InsetRect(teRect,2,2);
- teRect->right = teRect->right - 15;
- }
-
- /*********************************
- * AdjustVwRect subroutine
- *
- * subroutine description
- *
- * argument description
- *
- **********************************/
-
- void AdjustVwRect (TEHandle docTE)
- {
- TEPtr te;
-
- te = *docTE;
- te->viewRect.bottom = (((te->viewRect.bottom - te->viewRect.top)/
- te->lineHeight) * te->lineHeight) +
- te->viewRect.top;
- }
-
- /*********************************
- * AdjstHV subroutine
- *
- * subroutine description
- *
- * argument description
- *
- **********************************/
-
- void AdjstHV (Boolean Redraw)
- {
- short oldValue,oldMax,lines,max,value;
- TEPtr te;
-
- oldValue = GetCtlValue(docVScroll);
- oldMax = GetCtlMax(docVScroll);
- te = *docTE;
- lines = te->nLines;
- if (*(*te->hText + te->teLength - 1) == 13)
- lines += 1;
- max = lines - ((te->viewRect.bottom - te->viewRect.top)/
- te->lineHeight);
-
- if (max < 0) max = 0;
- SetCtlMax(docVScroll,max);
-
- te = *docTE;
- value = (te->viewRect.top - te->destRect.top)/te->lineHeight;
-
- if (value < 0) value = 0;
- else if (value > max) value = max;
-
- SetCtlValue(docVScroll,value);
- if (Redraw || (max != oldMax) || (value != oldValue))
- ShowControl(docVScroll);
- }
-
- /************************************
- * EvntLoop subroutine
- *
- * subroutine description
- *
- *************************************/
-
- void EvntLoop ()
- {
- EventRecord event;
- Boolean gotEvent;
- short part;
- WindowPtr window;
-
- void DoContentClick();
- void DoCloseWindow();
-
- do {
- SystemTask();
- gotEvent = GetNextEvent(everyEvent,&event);
-
- if (gotEvent) {
- switch (event.what) {
- case mouseDown:
- part = FindWindow(event.where,&window);
- switch (part) {
- case inContent:
- DoContentClick(window,&event);
- break;
- case inGoAway:
- if (TrackGoAway(window,event.where))
- DoCloseWindow(window);
- break;
- }
- break;
- }
- }
- } while (part != inGoAway);
- }
-
- /**************************************
- * DoContentClick subroutine
- *
- * subroutine description
- *
- * argument descriptions
- *
- ***************************************/
-
- void DoContentClick (WindowPtr window,EventRecord *event)
- {
- Point mouse;
- Rect teRect;
- ControlHandle control;
- short part,value;
-
- void GetTRect();
- pascal void VActnProc();
-
- SetPort(window);
- mouse = event->where;
- GlobalToLocal(&mouse);
- GetTRect(window,&teRect);
- if (!(PtInRect(mouse,&teRect))) {
- part = FindControl(mouse,window,&control);
- switch (part) {
- case 0:
- break;
- case inThumb:
- value = GetCtlValue(control);
- part = TrackControl(control,mouse,NULL);
- if (part != 0) {
- value -= GetCtlValue(control);
- if (value != 0)
- TEScroll(0,value*(*docTE)->lineHeight,docTE);
- }
- break;
- default:
- value = TrackControl(control,mouse,(ProcPtr) VActnProc);
- break;
- }
- }
- }
-
- /**********************************
- * VActnProc subroutine
- *
- * subroutine description
- *
- * argument descriptions
- *
- ***********************************/
-
- pascal void VActnProc (ControlHandle control,short part)
- {
- WindowPtr window;
- TEPtr te;
- short amount;
-
- void CommonAction();
-
- if (part != 0) {
- window = (*control)->contrlOwner;
- te = *docTE;
- switch (part) {
- case inUpButton:
- case inDownButton:
- amount = 1;
- break;
- case inPageUp:
- case inPageDown:
- amount = (te->viewRect.bottom - te->viewRect.top)/
- te->lineHeight;
- break;
- }
- if ((part == inDownButton) || (part == inPageDown))
- amount = -amount;
- CommonAction(control,&amount);
- if (amount != 0)
- TEScroll(0,amount*te->lineHeight,docTE);
- }
- }
-
- /*******************************
- * CommonAction subroutine
- *
- * subroutine description
- *
- * argument descriptions
- *
- ********************************/
-
- void CommonAction (ControlHandle control,short *amount)
- {
- short value,max;
-
- value = GetCtlValue(control);
- max = GetCtlMax(control);
- *amount = value - *amount;
- if (*amount < 0)
- *amount = 0;
- else if (*amount > max)
- *amount = max;
- SetCtlValue(control,*amount);
- *amount = value - *amount;
- }
-
- /**************************************
- * DoCloseWindow subroutine
- *
- * subroutine description
- *
- * argument description
- *
- ***************************************/
-
- void DoCloseWindow (WindowPtr window)
- {
- TEHandle te;
-
- te = docTE;
- if (te != NULL)
- TEDispose(te);
- DisposeWindow(window);
- }
-
- /*********************************
- * PascalClikLoop subroutine
- *
- * subroutine description
- *
- **********************************/
-
- pascal void PascalClikLoop ()
- {
- WindowPtr window;
- RgnHandle region;
-
-
- window = FrontWindow();
- region = NewRgn();
- GetClip(region);
- ClipRect(&window->portRect);
- AdjstHV(true);
- SetClip(region);
- DisposeRgn(region);
- }
-
- /**********************************
- * GetOldClikLoop subroutine
- *
- * subroutine description
- *
- ***********************************/
-
- pascal ProcPtr GetOldClikLoop ()
- {
- return docClik;
- }
-
-
- /*pascal void AsmClikLoop()
- {
-
- asm {
-
- MOVEM.L D1-D2/A1,-(SP) ; D0 and A0 need not be saved
- CLR.L -(SP) ; make space for procedure pointer
- JSR GetOldClikLoop ; get the old clikLoop
- MOVEA.L (SP)+,A0 ; into A0
- MOVEM.L (SP)+,D1-D2/A1 ; restore the world as it was
-
- JSR (A0) ; and execute old clikLoop
-
- MOVEM.L D1-D2/A1,-(SP) ; D0 and A0 need not be saved
- JSR PascalClikLoop ; do our clikLoop
- MOVEM.L (SP)+,D1-D2/A1 ; restore the world as it was
- MOVEQ #1,D0 ; clear the zero flag so TextEdit keeps going
- RTS
- }
- }
- */
-
-